package com.student.controller;



import com.student.pojo.Classes;
import com.student.pojo.Ke;
import com.student.pojo.ClassKe;
import com.student.service.ClassKeService;
import com.student.service.ClassesService;
import com.student.service.KeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
/**
 * @author 是一个鸽子啊
 * @date 2020/4/14
 * @qq 364826415
 */
@Controller
public class ClassKeController {
    @Autowired
    @Qualifier("KeServiceImpl")
    private KeService keService;
    @Autowired
    @Qualifier("classesServiceImpl")
    private ClassesService classesService;
    @Autowired
    @Qualifier("classKeServiceImpl")
    private ClassKeService classKeService;
    @RequestMapping("/queryClassKe")
    private String queryClassKe(HttpServletRequest request){
        List<ClassKe> classKeList = classKeService.queryClassKe();
        List<Ke> keList = keService.queryKe();
        List<Classes> classesList=classesService.queryClasses();
        for (ClassKe classKe : classKeList) {
            classKe.setClasses(classesService.queryClassesByID(classKe.getClassKeCID()));
            classKe.setKe(keService.queryKeByID(classKe.getClassKeKID()));
            classKe.setKeList(keList);
        }
        request.setAttribute("classKeList",classKeList);
        request.setAttribute("classesList",classesList);
        request.setAttribute("keList",keList);
        return "/class2-settle";

    }
    @RequestMapping("/updateClassKe")
    private String updateClassKe(ClassKe classKe,HttpServletRequest request){
        int i = classKeService.updateClassKe(classKe);
        List<ClassKe> classKeList1=new ArrayList<ClassKe>();
        List<Ke> keList = keService.queryKe();
        List<Classes> classesList=classesService.queryClasses();
        classKeList1= classKeService.queryClassKe();
        for (ClassKe classKe1 : classKeList1) {
            classKe1.setClasses(classesService.queryClassesByID(classKe1.getClassKeCID()));
            classKe1.setKe(keService.queryKeByID(classKe1.getClassKeKID()));
            classKe1.setKeList(keList);
        }
        request.setAttribute("classKeList",classKeList1);
        request.setAttribute("classesList",classesList);
        request.setAttribute("keList",keList);
        return "/class2-settle";

    }
    @RequestMapping("/queryClassKeByClassName")
    private String queryClassKeByClassName(HttpServletRequest request,String className){
        List<Classes> classesList = classesService.queryClassesByClassName(className);
        List<ClassKe> classKeList=new ArrayList<ClassKe>();
        for (Classes classes : classesList) {
            List<ClassKe> classKes = classKeService.queryClassKeByClassesID(classes.getClassID());
            for (ClassKe classKe : classKes) {
                classKe.setClasses(classesService.queryClassesByID(classKe.getClassKeCID()));
                classKe.setKe(keService.queryKeByID(classKe.getClassKeKID()));
                classKeList.add(classKe);
            }
        }
        request.setAttribute("classKeList",classKeList);
        return "/class2-settle";
    }
    @RequestMapping("addClassesKe")
    private String addClassesKe(ClassKe classKe,HttpServletRequest request){
        classKeService.addClassesKe(classKe);
        List<ClassKe> classKeList = classKeService.queryClassKe();
        List<Ke> keList = keService.queryKe();
        List<Classes> classesList=classesService.queryClasses();
        for (ClassKe classKe1 : classKeList) {
            classKe1.setClasses(classesService.queryClassesByID(classKe1.getClassKeCID()));
            classKe1.setKe(keService.queryKeByID(classKe1.getClassKeKID()));
            classKe1.setKeList(keList);
        }
        request.setAttribute("classKeList",classKeList);
        request.setAttribute("classesList",classesList);
        request.setAttribute("keList",keList);
        return "/class2-settle";

    }
    @RequestMapping("/delClassesKe")
    private String delClassesKe(String classKeID,HttpServletRequest request){
        classKeService.delClassesKe(Integer.parseInt(classKeID));
        List<ClassKe> classKeList = classKeService.queryClassKe();
        List<Ke> keList = keService.queryKe();
        List<Classes> classesList=classesService.queryClasses();
        for (ClassKe classKe1 : classKeList) {
            classKe1.setClasses(classesService.queryClassesByID(classKe1.getClassKeCID()));
            classKe1.setKe(keService.queryKeByID(classKe1.getClassKeKID()));
            classKe1.setKeList(keList);
        }
        request.setAttribute("classKeList",classKeList);
        request.setAttribute("classesList",classesList);
        request.setAttribute("keList",keList);
        return "/class2-settle";

    }

}
